package com.example.choupaiapp.main.activity.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.example.choupaiapp.R;
import com.example.choupaiapp.common.adapter.BaseAdapterHelper;
import com.example.choupaiapp.common.adapter.QuickAdapter;
import com.example.choupaiapp.common.constant.Contacts;
import com.example.choupaiapp.common.finalokgo.callback.Callback;
import com.example.choupaiapp.main.bean.SendCodeBean;
import com.example.choupaiapp.common.finalokgo.request.RequestType;
import com.example.choupaiapp.common.ui.activity.CommonActivity;
import com.example.choupaiapp.common.util.ContextUtil;
import com.example.choupaiapp.common.util.Utils;
import com.example.choupaiapp.main.bean.ReportTypeBean;
import com.example.choupaiapp.main.constant.Url;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2019/11/19.
 */

public class ReportActivity extends CommonActivity {

    @BindView(R.id.gridview)
    GridView gridview;
    private String contentId;
    private String moduleString;
    private String type;
    private String byUserId;
    private String commentId;
    private QuickAdapter<ReportTypeBean> quickAdapter;
    private String report_name = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);
    }

    @Override
    protected void initView() {
        setTitle("举报");

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            MobclickAgent.onEvent(ContextUtil.getContext(), "reportID");
            contentId = bundle.getString("contentId");
            moduleString = bundle.getString("module");
            type = bundle.getString("type");
            byUserId = bundle.getString("byUserId");
            commentId = bundle.getString("commentId");
        }

        quickAdapter = new QuickAdapter<ReportTypeBean>(context, R.layout.item_report_type) {
            @Override
            protected void convert(BaseAdapterHelper helper, ReportTypeBean item) {
                TextView label = helper.getView(R.id.label);
                label.setText(item.getName());
                if (item.isSelect()) {
                    label.setSelected(true);
                } else {
                    label.setSelected(false);
                }

            }
        };
        gridview.setAdapter(quickAdapter);


        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                for (int j = 0; j < quickAdapter.getCount(); j++) {
                    if (j == i) {
                        report_name = quickAdapter.getItem(i).getName();
                        quickAdapter.getItem(i).setSelect(true);
                    } else {
                        quickAdapter.getItem(j).setSelect(false);
                    }
                }
                quickAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {
        List<ReportTypeBean> list = new ArrayList<>();

        list.add(new ReportTypeBean("垃圾营销",false));
        list.add(new ReportTypeBean("涉黄信息",false));
        list.add(new ReportTypeBean("有害信息",false));
        list.add(new ReportTypeBean("违法信息",false));
        list.add(new ReportTypeBean("人身攻击",false));
        list.add(new ReportTypeBean("其他",false));

        quickAdapter.addAll(list);
    }

//    @Override
    @OnClick({R.id.submit})
    public void onViewClicked(View view) {
//        super.onViewClicked(view);
        if (Utils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.submit:
                if (!TextUtils.isEmpty(report_name)) {
                    showDialog(context, "举报中...");
                    JSONObject jsonObject = new JSONObject();
                    try {
                        jsonObject.put("contentId", contentId);

                        jsonObject.put("report", report_name);
                        jsonObject.put("type", type);
                        if (type.equals("2")) {
                            jsonObject.put("commentId", commentId);
                        }
                        jsonObject.put("byUserId", byUserId);
                        if ("1".equals(moduleString) || "0".equals(moduleString)) {
                            jsonObject.put("module", 1);
                        } else {
                            jsonObject.put("module", 2);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    finalOkGo.requestHead(RequestType.POSTJSONHEAD, Url.REPORT, null, jsonObject.toString(), new Callback<SendCodeBean>() {
                        @Override
                        public void onSuccess(SendCodeBean sendCodeBean) {
                            dismissDialog();
                            if (sendCodeBean.getCode() == Contacts.SUCCESS) {
                                showToast("感谢您的反馈，我们会对该内容进行核实处理");
                                finishActivity();
                            } else {
                                if (sendCodeBean.getCode() == Contacts.TOKEN_EMPTY) {
                                } else {
                                    showImageToas(sendCodeBean.getMsg());
                                }
                            }
                        }

                        @Override
                        public void onError(Response response) {
                            super.onError(response);
                            dismissDialog();
                        }
                    });
                } else {
                    showImageToas("请选择举报类型");
                }

                break;
        }
    }
}
